<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv=X-UA-Compatible content="IE=edge"/>
    <meta name=viewport content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>网上选课</title>
    <link rel="stylesheet" href="../css/element/index.css"/>
    <link rel="stylesheet" href="../css/main.css"/>
    <link rel="stylesheet" href="../css/header.css"/>
    <link rel="stylesheet" href="../css/sidebar.css"/>
    <link rel="stylesheet" href="../css/right.css"/>
</head>
<body>
<!--课程-->
<div id="app">
    <!-- 表格开始 -->
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-s-grid"></i>可选课程信息表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <!-- 表主体开始 -->
        <div class="container">
            <!-- 搜索开始 -->
            <div class="handle-box">
                <el-form :inline="true">
                    <el-select v-model="formMap.type" placeholder="选择课程类型" class="handle-select mr10">
                        <el-option key="1" label="必修" value="必修"></el-option>
                        <el-option key="2" label="选修" value="选修"></el-option>
                    </el-select>
                    <el-input v-model="formMap.name" placeholder="课程名称" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="fetchData" size="small">搜索</el-button>
                    </el-button>
                </el-form>
            </div>
            <!-- 搜索结束 -->
            <!-- 表格数据开始 -->
            <el-table ref="multipleTable"
                      :data="tableData" class="table"
                      tooltip-effect="dark" style="width: 100%"
                      @selection-change="handleSelectionChange">
                <!-- 多选框所在的列 -->
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="cno"
                        label="课程号"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="cname"
                        label="课程名"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="type"
                        label="课程类型"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="teacher"
                        label="授课教师"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="dengJi"
                        label="等级"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="kind"
                        label="种类"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="state"
                        label="状态"
                        width="140">
                    <template slot-scope="scope">
                       <span style="margin-left: 10px">{{scope.row.state==0?"未选":"已选"}}
                       </span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="150">
                    <template slot-scope="scope">
                        <el-button  type="primary" plain size="mini" @click="edit(scope.row)">选课
                        </el-button>
                        <el-button size="mini"  type="danger" plain @click="del(scope.row)">取消</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 表格数据结束 -->

            <!-- 分页开始 -->
            <div class="pagination">
                <el-pagination
                        @size-change="fetchData"
                        @current-change="fetchData"
                        :current-page.sync="currentPage"
                        :page-sizes="pageSizes"
                        :page-size.sync="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
            <!-- 分页结束 -->
        </div>
        <!-- 表主体结束 -->
    </div>
    <!-- 表格结束 -->
</div>


<script src="../js/vue.min.js"></script>
<script src="../js/element.js"></script>
<script src="../js/axios.min.js"></script>
<script src="../js/basic.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            tableData: [],
            multipleSelection: [],
            nowSno: '',//当前学号
            nowCno: '',//当前课程号
            formWidth: '120px',
            currentPage: 1, // 当前页码
            pageSize: 5, // 每页显示记录数
            pageSizes: [2, 3, 5, 10, 15, 20], // 选择每页显示数
            total: 0, // 总记录数
            formMap: {}, // 用于搜索的对象
        },
        created: function () {
            //获取当前学生号
            this.nowSno = localStorage.getItem("sNo");
            // 异步获取数据
            this.fetchData();
        },
        computed: {//计算
        },
        methods: {
            // 获取数据
            fetchData() {
                _this = this;
                this.formMap.dengJi = localStorage.getItem("dengJi");
                this.formMap.sNo = localStorage.getItem("sNo");
                axios.post(`${httpUrl}/student/courseList?page=${this.currentPage}&size=${this.pageSize}`, this.formMap)
                    .then(res => {
                        _this.tableData = res.data.row;
                        _this.total = res.data.total;
                    }).catch(err => {
                    this.$message({
                        showClose: true,
                        message: err.message,
                        type: 'error'
                    });
                });
            },
            //选课
            edit(row) {
                //判断点击的课程是否是已选课程
                if (row.state === 1) {
                    this.$message({
                        showClose: true,
                        message: "该课程已选，不可重复选择",
                        type: 'error'
                    });
                } else if (row.teacher === null || row.teacher === "暂无") {
                    this.$message({
                        showClose: true,
                        message: "该课程还未指定授课教师，暂时不可选择",
                        type: 'error'
                    });
                } else {
                    this.nowCno = row.cno;
                    _this = this;
                    axios.post(`${httpUrl}/student/xuanke?cNo=${this.nowCno}&sNo=${this.nowSno}`)
                        .then(res => {
                            if (res.data.code === 200) {
                                this.$message({
                                    showClose: true,
                                    message: res.data.msg,
                                    type: 'success'
                                });
                            } else {
                                this.$message({
                                    showClose: true,
                                    message: res.data.msg,
                                    type: 'error'
                                });
                            }
                            // 重新获取一次数据
                            _this.fetchData();
                        }).catch(er => {
                        this.$message({
                            showClose: true,
                            message: er.message,
                            type: 'error'
                        });
                    })
                }
            },
            //取消选课
            del(row) {
                if (row.state === 0) {
                    this.$message({
                        type: 'error',
                        message: "抱歉！该课程还未选，不能取消！！"
                    });
                } else if (row.type === "选修" && row.state !== 0) {
                    this.nowCno = row.cno;
                    _this = this;
                    this.$confirm('你确定要取消选择该门课程么？', '取消确认', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.get(`${httpUrl}/student/delXuanKe?cNo=${this.nowCno}&sNo=${this.nowSno}`)
                            .then(res => {
                                if (res.data.code === 200) {
                                    this.$message({
                                        type: 'success',
                                        message: res.data.msg
                                    });
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.msg
                                    });
                                }
                                _this.fetchData();
                            });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消'
                        });
                    });
                } else {
                    this.$message({
                        type: 'error',
                        message: "抱歉！不能取消选择必修课程！！"
                    });
                }
            },
            // 搜索
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    })
</script>
</body>
</html>